package myJs

import myJs.Utils._
import myJs.myPkg._
import org.scalajs.dom.{Element, Event, KeyboardEvent, document}
import shared.Implicits._

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.Implicits._
import myJs.myPkg.jquery._
import scala.scalajs.js.Dynamic.{global => g}
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 2019/4/15
 */
@JSExportTopLevel("Predict")
object Predict {

  @JSExport("init")
  def init = {
    //    showResult("322")
    registerEvent
    bootStrapValidator

  }

  def registerEvent = {
    $(document.body).on("click", s".myReduceNum", "", (y: Element) => {
      val textJq = $(y).parent().next()
      val value = textJq.`val`().toString.toInt
      textJq.`val`(s"${value - 1}")
      val bv = $("#form").bv
      bv.revalidateField(textJq)
    })
    $(document.body).on("click", s".myAddNum", "", (y: Element) => {
      val textJq = $(y).parent().prev()
      val value = textJq.`val`().toString.toInt
      textJq.`val`(s"${value + 1}")
      val bv = $("#form").bv
      bv.revalidateField(textJq)
    })
    $("#form :input").keydown((e: Event) => {
      val event = e.asInstanceOf[KeyboardEvent]
      if (event.keyCode == 13) {
        myRun
        false
      }else true
    })

  }

  @JSExport("myRun")
  def myRun = {
    val bv = $("#form").bv
    bv.validate()
    val valid = bv.isValid()
    if (valid) {
      val data = $("#form").serialize()
      val url = g.jsRoutes.controllers.PredictController.predict().url.toString
      val index = layer.alert(element, layerOptions)
      MyFetch.post(url = s"${url}", data = data, headers = Map("Content-Type" -> "application/x-www-form-urlencoded")).
        map { tmpRs =>
          layer.close(index)
          val rs = tmpRs.asInstanceOf[js.Dictionary[js.Any]]
          if (rs.myGet("valid") == "false") {
            Swal.swal(SwalOptions.title("错误").text(rs.myGet("message")).`type`("error"))
            $("#result").hide()
          } else {
            val missionId = rs.myGet("missionId")
            PredictResult.showResult(missionId)
          }
        }.timeoutDeal(index)
    }

  }

  def bootStrapValidator = {
    val url = g.jsRoutes.controllers.AdminController.userNameCheck().url.toString
    val dict = js.Dictionary(
      "feedbackIcons" -> js.Dictionary(
        "valid" -> "glyphicon glyphicon-ok",
        "invalid" -> "",
        "validating" -> "glyphicon glyphicon-refresh",
      ),
      "fields" -> js.Dictionary(
        "sampleId" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "样本号不能为空！"
            ),
          )
        ),
        "age" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "年龄不能为空！",
            ),
            "between" -> js.Dictionary(
              "min" -> 18,
              "max" -> 100,
              "message" -> "年龄必须处于18到100之间！"
            ),
            "integer" -> js.Dictionary(
              "message" -> s"年龄必须为整数！",
            ),
          )
        ),
        "ast" -> {
          val info="天门冬氨酸氨基转移酶"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> "天门冬氨酸氨基转移酶不能为空！"
              ),
              "between" -> js.Dictionary(
                "min" -> 0,
                "max" -> 1000,
                "message" -> "天门冬氨酸氨基转移酶必须处于0到1000之间！"
              ),
              "regexp" -> js.Dictionary(
                "message" -> s"${info}最多包含三位小数！",
                "regexp" -> js.RegExp("^\\d*(\\.\\d{1,3})?$"),
              ),
            )
          )
        },
        "alt" ->{
          val info="丙氨酸氨基转移酶"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> "丙氨酸氨基转移酶不能为空！"
              ),
              "between" -> js.Dictionary(
                "min" -> 0,
                "max" -> 2000,
                "message" -> "丙氨酸氨基转移酶必须处于0到2000之间！"
              ),
              "regexp" -> js.Dictionary(
                "message" -> s"${info}最多包含三位小数！",
                "regexp" -> js.RegExp("^\\d*(\\.\\d{1,3})?$"),
              ),
            )
          )
        } ,
        "plt" ->{
          val info="血小板计数"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> "血小板计数不能为空！"
              ),
              "between" -> js.Dictionary(
                "min" -> 0,
                "max" -> 1000,
                "message" -> "血小板计数必须处于0到1000之间！"
              ),
              "regexp" -> js.Dictionary(
                "message" -> s"${info}最多包含三位小数！",
                "regexp" -> js.RegExp("^\\d*(\\.\\d{1,3})?$"),
              ),
            )
          )
        } ,
        "tyr" ->{
          val info="L-酪氨酸"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> "L-酪氨酸不能为空！"
              ),
              "numeric" -> js.Dictionary(
                "message" -> "L-酪氨酸必须为实数！"
              ),
              "regexp" -> js.Dictionary(
                "message" -> s"${info}最多包含三位小数！",
                "regexp" -> js.RegExp("^\\d*(\\.\\d{1,3})?$"),
              ),
            )
          )
        } ,
        "tca" -> {
          val info="牛磺胆酸"
          js.Dictionary(
            "validators" -> js.Dictionary(
              "notEmpty" -> js.Dictionary(
                "message" -> "牛磺胆酸不能为空！"
              ),
              "numeric" -> js.Dictionary(
                "message" -> "牛磺胆酸必须为实数！"
              ),
              "regexp" -> js.Dictionary(
                "message" -> s"${info}最多包含三位小数！",
                "regexp" -> js.RegExp("^\\d*(\\.\\d{1,3})?$"),
              ),
            )
          )
        },
      )
    )
    g.$("#form").bootstrapValidator(dict)
  }

}
